package cn.edu.imnu.controller;

import cn.edu.imnu.common.ResponseData;
import cn.edu.imnu.entry.dto.AccountLoginDto;
import cn.edu.imnu.entry.vo.AccountLoginVo;
import cn.edu.imnu.service.AccountService;
import cn.edu.imnu.util.JwtTokenUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Objects;

@RestController
@RequestMapping("account")
public class AccountController {
    @Resource
    private AccountService accountService;

    @PostMapping("login")
    public ResponseData<AccountLoginVo> login(@RequestBody AccountLoginDto accountLoginDto){
        AccountLoginVo account = accountService.login(accountLoginDto);
        if(Objects.isNull(account)){
            return ResponseData.warn("用户名或密码错误");
        }
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", account.getId());
        account.setToken(JwtTokenUtil.createToken(claims));
        return ResponseData.ok(account);
    }
}
